package com.csii.mobilebanking.expandablelistview;


import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import com.csii.mobilebanking.R;
import com.csii.mobilebanking.expandablelistview.fragment.BaseFragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/***
 * 主要的一个菜单框架Activity
 * 
 * @author Hua
 * 
 */
@SuppressLint("UseSparseArrays")
public class FragmentActivity extends Activity {
	private static final String MAIN_ACTIVITY = "FragmentActivity";

	

	private static final Map<Integer, BaseFragment> FRAGMAENTS = new HashMap<Integer, BaseFragment>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_activity);
		
	}

	/***
	 * 
	 * @author Hua
	 * 
	 */
	public final static class TitleFragment extends ListFragment {
		
		private boolean mDualPane;
		private int mCurCheckPosition = 0;// 保存当前选中的下标
		private   TextView tv_title;
		private ArrayAdapter<String> adapter;

		@Override
		public void onAttach(Activity activity) {
			super.onAttach(activity);
			Log.d(MAIN_ACTIVITY, "onAttach...." + activity.getClass().getName());
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			Log.d(MAIN_ACTIVITY, "onCreate....");
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(MAIN_ACTIVITY, "onCreateView....");
			return super.onCreateView(inflater, container, savedInstanceState);
		}

		/***
		 * 初始化 选项
		 */
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			Log.d(MAIN_ACTIVITY, "onActivityCreated....");
			super.onActivityCreated(savedInstanceState);

			adapter = new ArrayAdapter<String>(getActivity(),
					R.layout.list_item, R.id.tv_title, getResources()
							.getStringArray(R.array.list_titlle));

			setListAdapter(adapter);
			// 设置分隔线
			getListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.list_bg));
			
			getListView().setDivider(
					getResources().getDrawable(R.color.list_divider));
			getListView().setDividerHeight(1);
			getListView().setSelector(R.drawable.list_bg_selector);
			if (savedInstanceState != null) {
				mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
			}
			// 解决列表出现黑色
			getListView().setCacheColorHint(android.R.color.white);
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			
			View detailsFrame = getActivity()
					.findViewById(R.id.fmt_right_title);
			mDualPane = detailsFrame != null
					&& detailsFrame.getVisibility() == View.VISIBLE;
			
			tv_title = (TextView) getActivity().findViewById(R.id.tv_right_title);
			showFragment(mCurCheckPosition);

		}

		/***
		 * ListView 选中下标时
		 * 
		 * @param index
		 */
		private void showFragment(int index) {
			Log.d(MAIN_ACTIVITY,adapter.getItem(index));
			//设置右边标题
			if(tv_title!=null){
				tv_title.setText(adapter.getItem(index));
			}
			
			mCurCheckPosition = index;
			if (mDualPane) {

				getListView().setItemChecked(index, true);

				BaseFragment details = (BaseFragment) getFragmentManager()
						.findFragmentById(R.id.fmt_right_title);

				if (details == null || details.getShownIndex() != index) {
					if (FRAGMAENTS.containsKey(index)) {
						details = FRAGMAENTS.get(index);
					} else {
						details = BaseFragment.newInstance(mCurCheckPosition);
						FRAGMAENTS.put(index, details);
					}

					// 得到一个fragment 事务（类似sqlite的操作）
					FragmentTransaction ft = getFragmentManager()
							.beginTransaction();
					ft.replace(R.id.fmt_right_title, details);// 将得到的fragment
					// 替换当前的viewGroup内容，add则不替换会依次累加
					ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);// 设置动画效果
					ft.commit();// 提交

				} else {
					// 点击当前的

				}
			}
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			Log.d(MAIN_ACTIVITY, "当前下标:" + position);

			showFragment(position);

		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putInt("curChoice", mCurCheckPosition);// 保存当前的下标
		}

	}

}
